var nv=new Vue({
    el:"#newVenueDiv",
    data:{
        venue:{},
        avatarImg:"",
        image1Img:"",
        image2Img:"",
        image3Img:"",
        nickname:"",
        avatar:"",
        image1:"",
        image2:"",
        image3:"",
        nicknameFlag:false,
        avatarFlag:false,
        image1Flag:false,
        image2Flag:false,
        image3Flag:false,
        accessCode:0
    },
    mounted(){
        axios.get("/venue/session").then(res=>{
            console.info("sessionVenue:")
            console.info(res.data.data);
            if (res.data.data===null){
                alert("还没注册呢")
            }
            this.venue=res.data.data;
        }).catch(err=>{
            console.info(err)});
        // let url=window.location.href;
        // console.info("url:"+url);
        // let vname=url.substring(url.lastIndexOf("=")+1);
        // axios.get("/venue/vname/"+vname).then(res=>{
        //     console.info(res)
        //     this.venue=res.data.data;
        //     console.info(this.venue)
        // }).catch(err=>{
        //     console.info(err)
        // })
    },
    methods:{
        showAvatarImg:function (e) {
            if (e.target.value===""){
                e.target.nextElementSibling.src="images\\default.png";
                this.avatarImg="";
                return;
            }
            this.imageReaderAndShow(e)
            this.avatarImg=e.target.files[0];

        },
        showImage1Img:function (e) {
            if (e.target.value===""){
                e.target.nextElementSibling.src="images\\default.png";
                this.image1Img="";
                return;
            }
            this.imageReaderAndShow(e)
            this.image1Img=e.target.files[0];
        },
        showImage2Img:function (e) {
            if (e.target.value===""){
                e.target.nextElementSibling.src="images\\default.png";
                this.image2Img="";
                return;
            }
            this.imageReaderAndShow(e)
            this.image2Img=e.target.files[0];
        },
        showImage3Img:function (e) {
            if (e.target.value===""){
                e.target.nextElementSibling.src="images\\default.png";
                this.image3Img="";
                return;
            }
            this.imageReaderAndShow(e)
            this.image3Img=e.target.files[0];
        },
        imageReaderAndShow:function (e) {
            let fileReader = new FileReader();
            fileReader.readAsDataURL(e.target.files[0]);
            fileReader.onload=function(){
                e.target.nextElementSibling.src=fileReader.result;
            }
        },
        uploadNickname:function(){
            console.info("场馆名："+this.nickname);
            //注册场馆名
            return axios({
                method: 'post',
                url: "/venue/complete",
                data: {
                    "vid":this.venue.vid,
                    "nickname":this.nickname
                }
            })
            //     .then(res => {
            //     console.info(res)
            //     console.info(this.nicknameFlag)
            //     this.nicknameFlag=res.data.code===200;
            //     console.info(this.nicknameFlag)
            //     if (res.data.code===300){
            //         console.info("进来了")
            //         this.accessCode++;
            //     }
            // })
            //     .catch(err => {
            //     });
        },
        uploadAvatar:function(){
            //上传头像
            let formDataA = new FormData();
            formDataA.append('type',this.avatarImg.type);
            formDataA.append('name',this.avatarImg.name);
            formDataA.append('vid',this.venue.vid);
            formDataA.append('who',"0");
            formDataA.append('data',this.avatarImg);
            return axios({
                method: 'post',
                url: "/venue/image",
                data: formDataA,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
            //     .then(res => {
            //     console.info(res)
            //     this.avatarFlag=res.data.code===200;
            //     if (res.data.code===300){
            //         this.accessCode++;
            //     }
            // })
            //     .catch(err => {
            //     });
        },
        uploadImage1:function(){
            //上传展示图片1
            let formData1 = new FormData();
            formData1.append('type',this.image1Img.type);
            formData1.append('name',this.image1Img.name);
            formData1.append('vid',this.venue.vid);
            formData1.append('who',"1");
            formData1.append('data',this.image1Img);
            return axios({
                method: 'post',
                url: "/venue/image",
                data: formData1,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
            //     .then(res => {
            //     console.info(res)
            //     this.image1Flag=res.data.code===200;
            //     if (res.data.code===300){
            //         this.accessCode++;
            //     }
            // })
            //     .catch(err => {
            //     });
        },
        uploadImage2:function(){
            //上传展示图片2
            let formData2  = new FormData();
            formData2.append('type',this.image2Img.type);
            formData2.append('name',this.image2Img.name);
            formData2.append('vid',this.venue.vid);
            formData2.append('who',"2");
            formData2.append('data',this.image2Img);
            return axios({
                method: 'post',
                url: "/venue/image",
                data: formData2,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
            //     .then(res => {
            //     console.info(res)
            //     this.image2Flag=res.data.code===200;
            //     if (res.data.code===300){
            //         this.accessCode++;
            //     }
            // })
            //     .catch(err => {
            //     });
        },
        uploadImage3:function(){
            //上传展示图片3
            let formData3 = new FormData();
            formData3.append('type',this.image3Img.type);
            formData3.append('name',this.image3Img.name);
            formData3.append('vid',this.venue.vid);
            formData3.append('who',"3");
            formData3.append('data',this.image3Img);
            return axios({
                method: 'post',
                url: "/venue/image",
                data: formData3,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
            //     .then(res => {
            //     console.info(res)
            //     this.image3Flag=res.data.code===200;
            //     if (res.data.code===300){
            //         this.accessCode++;
            //     }
            // })
            //     .catch(err => {
            //     });
        },
        submitAll:function () {
            if (!confirm("确认填写完了所有的信息？")){
                console.info("继续填吧小伙子")
                return;
            }
            // if (this.nickname.trim()===""||this.avatar===""||this.image1===""||this.image2===""||this.image3===""){
            //     console.info("请填写所有的信息")
            //     alert("请填写所有的信息")
            //     return;
            // }
            console.info(this.avatarImg)
            console.info(this.image3Img)
            console.info(this.image2Img)
            console.info(this.image1Img)
            if (this.avatarImg===null||this.image1Img===null||this.image2Img===null||this.image3Img===null){
                console.info("文件不能为空啊老弟")
                return;
            }
            //所有上传成功才算成功
            //并发请求
            axios.all([this.uploadNickname(), this.uploadAvatar(),this.uploadImage1(),this.uploadImage2(),this.uploadImage3()])
                .then(axios.spread((n,a,img1,img2,img3)=>{
                    // 所有请求现在都执行完成
                    console.info(n)
                    console.info(a)
                    console.info(img1)
                    console.info(img2)
                    console.info(img3)
                    console.info(n.data.code)
                    console.info(a.data.code)
                    console.info(img1.data.code)
                    console.info(img2.data.code)
                    console.info(img3.data.code)
                    if (n.data.code===200&&a.data.code===200&&img1.data.code===200&&img2.data.code===200&&img3.data.code===200){
                        alert("注册成功！");
                        window.location.href="homePage.html";
                    }else{
                        alert("注册失败，请重新填写信息");
                    }
                }));

            // console.info(this.nicknameFlag)
            // console.info(this.avatarFlag)
            // console.info(this.image1Flag)
            // console.info(this.image2Flag)
            // console.info(this.image3Flag)
            // if (this.nicknameFlag&&this.avatarFlag&&this.image1Flag&&this.image2Flag&&this.image3Flag){
            // if (this.accessCode===0){
            //     alert("注册成功！");
            //     window.location.href="homePage.html";
            // }else{
            //     alert("注册失败，请重新填写信息");
            // }
        }//submitAll end

    }//methods end
})